<?php

namespace App\Admin\Forms;

use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\DB;

class AssetsForm extends Form implements LazyRenderable
{
    use LazyWidget;
    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        $id = $this->payload['id'];

        $result = false;
        if ($input['fund_type']==1 && $input['change_type']==1){
            $result =  DB::table('users')->where('id',$id)->increment('balance',$input['change_balance']);

            DB::table('user_balance_details')
                ->insert([
                    'user_id' => $id,
                    'change_type' => $input['change_type'],
                    'amount' => $input['change_balance'],
                    'remark' => $input['remark'] ?? '平台增加',
                    'change_time' => date('Y-m-d H:i:s')
                ]);
        }


        if ($input['fund_type']==1 && $input['change_type']==2){
            if ($input['change_balance']>$this->payload['balance']){
                return  $this->response()->error('当前用户余额不足无法减少');
            }

            $result =  DB::table('users')->where('id',$id)->decrement('balance',$input['change_balance']);



            DB::table('user_balance_details')
                ->insert([
                    'user_id' => $id,
                    'change_type' => $input['change_type'],
                    'amount' => $input['change_balance'],
                    'remark' => $input['remark'] ?? '平台减少',
                    'change_time' => date('Y-m-d H:i:s')
                ]);
        }





        if ($input['fund_type']==2 && $input['change_type']==1){
            $result =  DB::table('users')->where('id',$id)->increment('points',$input['change_points']);
            DB::table('user_points_details')
                ->insert([
                    'user_id' => $id,
                    'change_type' => $input['change_type'],
                    'points' => $input['change_points'],
                    'remark' => $input['remark'] ?? '平台增加',
                    'change_time' => date('Y-m-d H:i:s'),
                ]);
        }


        if ($input['fund_type']==2 && $input['change_type']==2){
            if ($input['change_points']>$this->payload['points']){
                return  $this->response()->error('当前用户积分不足无法减少');
            }


            $result =  DB::table('users')->where('id',$id)->decrement('points',$input['change_points']);

            DB::table('user_points_details')
                ->insert([
                    'user_id' => $id,
                    'change_type' => $input['change_type'],
                    'points' => $input['change_points'],
                    'remark' => $input['remark'] ?? '平台减少',
                    'change_time' => date('Y-m-d H:i:s'),
                ]);
        }


        if ($result){

            return  $this->response()
                ->success('操作成功')
                ->refresh() ;
        }

        return $this->response()->error('操作失败')->refresh();



    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->radio('fund_type','资金类型')
            ->options([1 => '余额',2 => '积分'])
            ->when(1,function (Form $form){
                $form->display('balance','当前余额')->default($this->payload['balance']);
                $this->currency('change_balance','变动余额')->symbol('￥')
                     ->rules('required_if:fund_type,1',['required_if' => '变动余额不能为空']);
            })->when(2,function (Form $form){
                $form->display('points','当前积分')->default($this->payload['points']);
                $form->number('change_points','变动积分')
                     ->min(0)
                     ->rules('required_if:fund_type,2',['required_if' => '变动积分不能为空']);
            })->default(1);


        $this->radio('change_type','变化类型')
            ->options([1 => '增加',2 => '减少'])
            ->default(1)
            ->required();

        $this->textarea('remark','备注');


    }

}
